package com.geo.ai.model;

import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * AI响应模型
 * 
 * @author GEO Team
 * @version 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AIResponse {
    
    /**
     * 请求ID
     */
    private String requestId;
    
    /**
     * 响应ID
     */
    private String responseId;
    
    /**
     * 模型名称
     */
    private String modelName;
    
    /**
     * 响应状态
     */
    private ResponseStatus status;
    
    /**
     * 生成的内容
     */
    private String content;
    
    /**
     * 使用的token数量
     */
    private TokenUsage tokenUsage;
    
    /**
     * 响应时间（毫秒）
     */
    private Long responseTimeMs;
    
    /**
     * 生成成本
     */
    private Double cost;
    
    /**
     * 错误信息
     */
    private String errorMessage;
    
    /**
     * 错误代码
     */
    private String errorCode;
    
    /**
     * 额外元数据
     */
    private Map<String, Object> metadata;
    
    /**
     * 响应时间
     */
    private LocalDateTime responseTime;
    
    /**
     * 完成原因
     */
    private FinishReason finishReason;
    
    /**
     * 响应状态枚举
     */
    public enum ResponseStatus {
        SUCCESS("成功"),
        PARTIAL_SUCCESS("部分成功"),
        FAILED("失败"),
        TIMEOUT("超时"),
        RATE_LIMITED("限流"),
        INVALID_REQUEST("无效请求"),
        UNAUTHORIZED("未授权"),
        MODEL_UNAVAILABLE("模型不可用");
        
        private final String description;
        
        ResponseStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    /**
     * 完成原因枚举
     */
    public enum FinishReason {
        STOP("正常停止"),
        LENGTH("达到最大长度"),
        CONTENT_FILTER("内容过滤"),
        FUNCTION_CALL("函数调用"),
        ERROR("错误");
        
        private final String description;
        
        FinishReason(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    /**
     * Token使用统计
     */
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TokenUsage {
        /**
         * 输入token数
         */
        private Integer promptTokens;
        
        /**
         * 输出token数
         */
        private Integer completionTokens;
        
        /**
         * 总token数
         */
        private Integer totalTokens;
        
        /**
         * 计算总token数
         */
        public void calculateTotal() {
            if (promptTokens != null && completionTokens != null) {
                this.totalTokens = promptTokens + completionTokens;
            }
        }
        
        // 手动添加builder方法以解决编译问题
        public static TokenUsageBuilder builder() {
            return new TokenUsageBuilder();
        }
        
        public static class TokenUsageBuilder {
            private Integer promptTokens;
            private Integer completionTokens;
            private Integer totalTokens;
            
            public TokenUsageBuilder promptTokens(Integer promptTokens) {
                this.promptTokens = promptTokens;
                return this;
            }
            
            public TokenUsageBuilder completionTokens(Integer completionTokens) {
                this.completionTokens = completionTokens;
                return this;
            }
            
            public TokenUsageBuilder totalTokens(Integer totalTokens) {
                this.totalTokens = totalTokens;
                return this;
            }
            
            public TokenUsage build() {
                TokenUsage tokenUsage = new TokenUsage();
                tokenUsage.promptTokens = this.promptTokens;
                tokenUsage.completionTokens = this.completionTokens;
                tokenUsage.totalTokens = this.totalTokens;
                return tokenUsage;
            }
        }
        
        // 添加getTotalTokens方法
        public Integer getTotalTokens() {
            return totalTokens;
        }
    }
    
    /**
     * 检查响应是否成功
     * @return 是否成功
     */
    public boolean isSuccess() {
        return status == ResponseStatus.SUCCESS || status == ResponseStatus.PARTIAL_SUCCESS;
    }
    
    /**
     * 检查是否有错误
     * @return 是否有错误
     */
    public boolean hasError() {
        return !isSuccess();
    }
    
    // 添加缺失的getter方法
    public String getErrorMessage() {
        return errorMessage;
    }
    
    public String getContent() {
        return content;
    }
    
    public String getModelName() {
        return modelName;
    }
    
    public String getRequestId() {
        return requestId;
    }
    
    public Integer getTotalTokens() {
        return tokenUsage != null ? tokenUsage.totalTokens : null;
    }
    
    // 手动添加builder方法以解决编译问题
    public static AIResponseBuilder builder() {
        return new AIResponseBuilder();
    }
    
    public static class AIResponseBuilder {
        private String requestId;
        private String responseId;
        private String modelName;
        private ResponseStatus status;
        private String content;
        private TokenUsage tokenUsage;
        private Long responseTimeMs;
        private Double cost;
        private String errorMessage;
        private String errorCode;
        private Map<String, Object> metadata;
        private LocalDateTime responseTime;
        private FinishReason finishReason;
        
        public AIResponseBuilder requestId(String requestId) {
            this.requestId = requestId;
            return this;
        }
        
        public AIResponseBuilder responseId(String responseId) {
            this.responseId = responseId;
            return this;
        }
        
        public AIResponseBuilder modelName(String modelName) {
            this.modelName = modelName;
            return this;
        }
        
        public AIResponseBuilder status(ResponseStatus status) {
            this.status = status;
            return this;
        }
        
        public AIResponseBuilder content(String content) {
            this.content = content;
            return this;
        }
        
        public AIResponseBuilder tokenUsage(TokenUsage tokenUsage) {
            this.tokenUsage = tokenUsage;
            return this;
        }
        
        public AIResponseBuilder responseTimeMs(Long responseTimeMs) {
            this.responseTimeMs = responseTimeMs;
            return this;
        }
        
        public AIResponseBuilder cost(Double cost) {
            this.cost = cost;
            return this;
        }
        
        public AIResponseBuilder errorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
            return this;
        }
        
        public AIResponseBuilder errorCode(String errorCode) {
            this.errorCode = errorCode;
            return this;
        }
        
        public AIResponseBuilder metadata(Map<String, Object> metadata) {
            this.metadata = metadata;
            return this;
        }
        
        public AIResponseBuilder responseTime(LocalDateTime responseTime) {
            this.responseTime = responseTime;
            return this;
        }
        
        public AIResponseBuilder finishReason(FinishReason finishReason) {
            this.finishReason = finishReason;
            return this;
        }
        
        public AIResponse build() {
            AIResponse response = new AIResponse();
            response.requestId = this.requestId;
            response.responseId = this.responseId;
            response.modelName = this.modelName;
            response.status = this.status;
            response.content = this.content;
            response.tokenUsage = this.tokenUsage;
            response.responseTimeMs = this.responseTimeMs;
            response.cost = this.cost;
            response.errorMessage = this.errorMessage;
            response.errorCode = this.errorCode;
            response.metadata = this.metadata;
            response.responseTime = this.responseTime;
            response.finishReason = this.finishReason;
            return response;
        }
    }
}